package com.chianghao.qyt.auth;

import com.chianghao.frame.core.auth.Authentication;
import com.chianghao.frame.core.auth.Login;
import com.chianghao.frame.core.auth.impl.AuthenticationDefault;
import com.chianghao.frame.core.auth.jwt.JwtEntityDefault;
import com.chianghao.frame.core.auth.session.CHSessionRepository;
import com.chianghao.frame.core.auth.session.UserToSessionRepository;
import com.chianghao.frame.core.auth.whilte_list.WhiteListVerify;
import com.chianghao.frame.web.mvc.filter.AuthAbstractFilter;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AuthConfig {

    @Resource
    WhiteListVerify whiteListVerify;


    private final static String securityKey = "12345678qazwsxed";

    @Bean
    public Authentication manageSystemAuthentication(CHSessionRepository sessionRepository) {
        return new AuthenticationDefault(sessionRepository, securityKey);
    }

    @Bean
    public Login mesageLogin(
            @Autowired CHSessionRepository sessionRepository,
            UserToSessionRepository userToSessionRepository) {
        return new Login(securityKey, sessionRepository, userToSessionRepository);
    }


    @Bean
    public FilterRegistrationBean pagesFilterRegistration(Authentication manageSystemAuthentication) {
        FilterRegistrationBean<?> registrationBean = AuthAbstractFilter.createFilterRegistrationBean(
                whiteListVerify,
                manageSystemAuthentication,
                JwtEntityDefault.class,
                "*.html",
                "pagesFilterRegistration"
        );
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean apiManageFilterRegistration(Authentication manageSystemAuthentication) {
        FilterRegistrationBean<?> registrationBean = AuthAbstractFilter.createFilterRegistrationBean(
                whiteListVerify,
                manageSystemAuthentication,
                JwtEntityDefault.class,
                "/manage/api/*",
                "apiManageFilterRegistration"
        );
        return registrationBean;
    }

}
